

import { Transform, Widget } from "../index";
import { GameObject } from './gameobject';


export class Component {
    static typeName: string = "Component";
    name: string;
    parent: string;

    _node: GameObject;
    get node() {
        if (this._node == null) {
            this._node = this.getGameObject(this.parent);
        }
        return this._node;
    }
    onStart() { }

    onUpdate(dt: number) {

    }

    getComponent<T extends typeof Component>(value: T): InstanceType<T> {
        return Deno.core.ops.op_getComponent(this.parent, value.typeName);
    }

    getGameObject(name: string): GameObject {
        return Deno.core.ops.op_getGameObject(this, name);
    }

    getUiNode<T extends Widget>(name: string): T {
        return Deno.core.ops.opGetUiNode(name);
    }

    toString() {
        return "Component {}"
    }
}


//把组件实例暴露到全局作用域中，是为了方便rust中创建实例后设置原型对象
//exposing component instance to global scope
// to make it convenient to set prototype object after creating instance in rust
globalThis.__Component__ = new Component();
globalThis.__Components__ = [];

